import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Ad, Channel, ImageSlider, Product, TopMenu } from 'src/app/shared';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root', // 这句话是在 ng6 的时候才加上，更适合 treeShaking
})
export class HomeService {
  imageSliders: ImageSlider[] = [];

  channels: Channel[] = [];

  topMenus: TopMenu[] = [];

  constructor(private http: HttpClient) {}

  getBanners(): Observable<ImageSlider[]> {
    return this.http.get<ImageSlider[]>(`${environment.baseUrl}/banners`);
  }

  getChannels(): Observable<Channel[]> {
    return this.http.get<Channel[]>(`${environment.baseUrl}/channels`);
  }

  getTabs(): Observable<TopMenu[]> {
    return this.http.get<TopMenu[]>(`${environment.baseUrl}/tabs`);
  }

  getAdByTab(tab: string): Observable<Ad[]> {
    return this.http.get<Ad[]>(`${environment.baseUrl}/ads`, {
      params: {
        categories_like: tab,
      },
    });
  }

  getProductsByTab(tab: string): Observable<Product[]> {
    return this.http.get<Product[]>(`${environment.baseUrl}/products`, {
      params: {
        categories_like: tab,
      },
    });
  }
}
